<?php
/**
 * Klasse zur Ausgabe von Prospektinformationen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 *
 * */


Class Yapary_Page_Content
{
    /** @var $oSql Yapary_Database_Sql */
    private $oSql           = NULL;
    /** @var $arData Array */
    private $arrData        = array();
    /** @var  $dbTableName string */
    private $dbTableName    = 'yapary_page_content';
    /** @var int $elementId			: Id des Prospektes */
    private $elementId      = NULL;
    /** @var $arrValid array */
    private $arrValid = array();


    /**
     * @param Object $_oSql	: Datenbankklasse fuer Datenbankzugriff
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql, $_id)
    {
        $this->oSql        = $_oSql;

        if (is_object($_id)) {
            $this->arrData   = (array) $_id;
            $this->elementId = $_id->Id;
        } elseif (is_numeric($_id)) {
            $this->elementId   = $_id;
            if (!$this->load()) {
                $e = new Exception();
                trigger_error('<pre>'.$e->getTraceAsString().'<br /><b>'.$this->dbTableName.':</b> error while loading dataset <b>'.$_id.'</b></pre>', E_USER_WARNING);
            }
        } else {
            trigger_error('Invalid numeric value for Parameter 2 _id', E_USER_WARNING);
        }
    }


    /**
     * Liest die Datenbanktabelle aus uns speichert die Werte lokal ab
     * @return bool
     * */
    private function load ()
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.'
        					  WHERE Id=:id',
                            array('id' => $this->elementId));
        if ($oDbResult->getCount() == 0) {
            return false;
        }

        $this->arrData = $oDbResult->getList();
        return true;
    }


    /**
     * Speichert die Daten im Array in der Datenbank ab
     * @return bool
     * */
    public function save()
    {
        $result = false;
        $dbTableName = $this->dbTableName;
        $id          = $this->elementId;
        $query       = '';

        if (count($this->arrData) >= 1) {
            while(list($name, $value) = each($this->arrData)) {
                if (!is_numeric($name) && $name != 'Id' && $name != 'Timestamp') {
                    $query .= $name."='".$value."',";
                }
            }
            reset($this->arrData);

            $query = substr($query, 0, strlen($query)-1);
            $oDbResult = $this->oSql->request("UPDATE $dbTableName SET $query WHERE Id='$id'",
                                           array());
            return true;
        }
        
        return false;
    }


    /**
     * Hilfsmethode zur Ausgabe eines Wertes
     * @param string $valueName	: Name des Datenbankrelation
     * @return string|NULL
     * */
    private function getValue ($valueName)
    {
        if (!isset($this->arrData[$valueName])) {
            trigger_error('<b>'.$valueName.'</b > data not available', E_USER_WARNING);
            return NULL;
        }

        return $this->arrData[$valueName];
    }


    /**
     * Liefert die eigene Id
     * @return string
     * */
    public function getId () {
        return $this->getValue('Id');
    }


    /**
     * Liefert die eigene Id
     * @return string
     * */
    public function getPageId () {
        return $this->getValue('PageId');
    }

	/**
	* Setzt den Namen des Seiteneintrages.
	* @param string $_name	: Name der Kategorieebene
	* @return bool
	*/
	public function setPageId ($_id) {
        if (!is_numericg($_id)) {
            return $this->arrValid['PageContent PageId'] = false;
        }

        $this->arrData['Id'] = $_id;
        return $this->arrValid['PageContent Id'] = true;
	}


	/**
	* Liefert den Namen des Seiteneintrages.
	* @return String
	*/
	public function getName () {
        return $this->getValue('Name');
	}


	/**
	* Setzt den Namen des Seiteneintrages.
	* @param string $_name	: Name der Kategorieebene
	* @return bool
	*/
	public function setName ($_name) {
        if (!is_string($_name)) {
            return $this->arrValid['PageContent Name'] = false;
        }

        $this->arrData['Name'] = $_name;
        return $this->arrValid['PageContent Name'] = true;
	}


	/**
	* Liefert den Namen der Kategorieebene.
	* @return String
	*/
	public function getContent () {
        return $this->getValue('Content');
	}


	/**
	* Setzt den Namen der Kategorieebene.
	* @param string $_name	: Zu speichernder Content
	* @return bool
	*/
	public function setContent ($_name = 'content') {
        if (!is_string($_name)) {
            return $this->arrValid['PageContent Content'] = false;
        }

        $this->arrData['Content'] = $_name;
        return $this->arrValid['PageContent Content'] = true;
	}


	/**
	* Liefert den Namen der Person, welche das Bild hochgeladen hat.
	* @return string
	*/
	public function getAuthor () {
        return $this->getValue('Author');
	}


	/**
	* Setzt den Namen der Person, welche das Bild hochgeladen hat.
	* @param string $_name	: Name des Uploaders
	* @return bool
	*/
	public function setAuthor ($_name) {
        if (!is_string($_name)) {
            return $this->arrValid['Author'] = false;
        }

        $this->arrData['Author'] = $_name;
        return $this->arrValid['Author'] = true;
	}


	/**
	* Liefert das Erstellungsdatum anhand der Produktbild-Id.
	* @return datetime
	*/
	public function getTimestamp () {
        return (int) $this->getValue('Timestamp');
	}


	/**
	* Liefert die administrative Signierung.
	* @return bool
	*/
	public function getSignature () {
        return (int) $this->getValue('Signature');
	}


	/**
	* Setzt die administrative Signierung.
	* @param int $_id	: Id der Signatur
	* @return bool
	*/
	public function setSignature ($_nbr) {
        if (!is_numeric($_nbr)) {
            return $this->arrValid['Administrative Signatur'] = false;
        }

        $this->arrData['Signature'] = $_nbr;
        return $this->arrValid['Administrative Signatur'] = true;
	}


    /**
     * Prueft ob alle Eingaben Valide waren
     * @return bool
     * */
    public function valid ()
    {
        if (count($this->arrValid)) {
            foreach($this->arrValid as $key => $val) {
                if ($val === false) {
                    return false;
                }
            }
        }

        return true;
    }


}


?>